import {GetUserInfo, GetUserList, UserLogin, UserRegister} from "../../api/userApi";
import {Message, MessageBox} from "element-ui";
import {removeToken, setToken} from "../../utils/token";
import router from "../../router";

const user = {

    actions:{
        userRegister({commit} , userInfo){
            const {username, password,address} = userInfo;
            return new Promise((resolve, reject) => {
                UserRegister(userInfo).then(() =>{
                    resolve();
                }).catch(err =>{
                    Message.error("注册失败，请检查网络状况或者联系管理员 "+err);
                    reject();
                })
            })
        },
        /**
         * 用户登陆
         * @param commit
         * @param userInfo
         * @returns {Promise<unknown>}
         * @constructor
         */
        userLogin({commit}, userInfo) {
            const {userName, userPassword, captcha} = userInfo;
            return new Promise((resolve, reject) => {
                UserLogin(userInfo).then(res => {
                    if (res.data.errorCode === 20005){
                        Message.warning(res.data.errorMsg)
                        return reject();
                    }
                    if (res.data.errorCode === 20003){
                        Message.warning(res.data.errorMsg)
                        return reject();
                    }
                    if (res.data.errorCode === 10001) {
                        Message.warning(res.data.errorMsg)
                        return reject();
                    }
                    if (res.data.errorCode === 20001) {
                        Message.warning(res.data.errorMsg);
                        return reject();
                    }
                    if (res.data.errorCode === 20002) {
                        Message.warning(res.data.errorMsg);
                        return reject();
                    }
                    commit('Set_Token', res.data.token);
                    setToken(res.data.token);
                    resolve();
                }).catch(err => {
                    Message.error("登陆失败，请检查网络状况或者联系管理员");
                    reject();
                })
            })
        },

        /**
         * 获取用户信息
         * @param commit
         * @param token
         * @param state
         * @constructor
         */
        getUserInfo({commit}, token) {
            return new Promise((resolve, reject) => {
                GetUserInfo(token).then(res => {
                    commit('Set_Role',res.data.role)
                    resolve();
                }).catch(error => {
                    MessageBox.confirm("服务器出现异常，即将退出系统",'系统提醒',{
                        confirmButtonText: '重新登陆',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() =>{
                        removeToken();
                        location.reload()
                    })
                    reject();
                })
            })
        },

    }
}

export default user